using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Xsl;
using CSF.Communications.Sdp.ServiceLogic.Entities;
using CSF.Communications.Sdp.ServiceLogic.DataAccess;
using CSF.Communications.Sdp.ServiceLogic.Exceptions;
using System.Data;


namespace CSF.Communications.Sdp.ServiceLogic.Utilities
{
    public class DataServiceProvider
    {
        #region Private Members
        private readonly string connectionString;
        private ConfigurationProvider slConfig = null;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor which assigns the connection string.
        /// </summary>
        public DataServiceProvider()
        {
            try
            {
                slConfig = ConfigurationProvider.GetInstance();
                connectionString = slConfig.SL.DbConn;
            }
            catch (Exception dataServiceProviderException)
            {
                throw dataServiceProviderException;
            }
        }
        #endregion

        #region Methods

        #region InsertMidinDB
        /// <summary>
        /// 
        /// </summary>
        public void InsertMidinDB(SoapRequestState request, ServiceLogicPayloadType payload, CSF_MID mid, string rulePolicy, bool isUpdate)
        {

            DataAccess.DataServiceProvider dsp;
            try
            {

                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
                if (!isUpdate)
                    dsp.InsertSLPayLoad(request.SessionID, request.RequestUUID, mid.SerializeObject(), rulePolicy);
                else
                    dsp.UpdateMID(request.RequestUUID, mid.SerializeObject());
            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorInsertingMid), ex.Message);
            }


        }
        #endregion

        #region UpdateMidinDB
        /// <summary>
        /// 
        /// </summary>
        public void UpdateMidinDB(SoapRequestState request, CSF_MID mid)
        {

            DataAccess.DataServiceProvider dsp;
            try
            {
                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
                dsp.UpdateMID(request.SessionID, mid.SerializeObject());
            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorUpdatingMid), ex.Message);
            }

        }
        #endregion

        #region GetMidFromDB
        /// <summary>
        /// 
        /// </summary>
        public CSF_MID GetMidFromDB(SoapRequestState request)
        {

            DataAccess.DataServiceProvider dsp;
            DataSet ds;
            CSF_MID mid = null;

            try
            {

                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();

                ds = dsp.GetMid(request.RequestUUID);

                mid = CSF_MID.DeSerializeObject(ds.Tables[0].Rows[0][0].ToString());

            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorFetchingMid), ex.Message);
            }
            return mid;

        }
        #endregion

        #region ArchiveMid
        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        /// <param name="mid"></param>
        public void ArchiveMid(SoapRequestState state, CSF_MID mid)
        {

            DataAccess.DataServiceProvider dsp;
            DataSet ds;
            try
            {
                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
                dsp.UpdateMIDArchive(state.SessionID, mid.SerializeObject());

            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorArchivingMid), ex.Message);
            }
        }
        #endregion

        #region UpdateServiceLogicState
        /// <summary>
        /// 
        /// </summary>
        public void UpdateServiceLogicState(SoapRequestState request, ServiceLogicStateEnum state)
        {

            DataAccess.DataServiceProvider dsp;
            DataSet ds;
            try
            {
                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
                dsp.ChangeServiceLogicState(request.SessionID, request.RequestUUID, Convert.ToInt32(state));

            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorUpdatingServicelogicStatus), ex.Message);
            }
            finally
            {

            }


        }
        #endregion

        #region DeleteMidFromDB
        /// <summary>
        /// 
        /// </summary>
        public void DeleteMidFromDB(SoapRequestState request)
        {

            DataAccess.DataServiceProvider dsp;
            DataSet ds;
            try
            {
                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
                ds = dsp.DeleteMid(request.RequestUUID);

            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorDeletingMid), ex.Message);
            }
        }
        #endregion

        #region ChangeStatusinSBE
        /// <summary>
        /// ChangeStatusinSBE
        /// </summary>
        /// <param name="sessionID"></param>
        /// <param name="correlationID"></param>
        /// <param name="status"></param>
        public void ChangeStatusinSBE(string sessionID, string correlationID, int status)
        {
            CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider dsp;
            try
            {
                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorUpdatingServicelogicStatus), ex.Message);
            }

        }
        #endregion

        #region GetPolicyFromDB
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public string GetPolicyFromDB(CSF_MID mid)
        {
            string productOfferingID = mid.CSFData.ProductOfferingID;
            string action = mid.CSFData.Action;
            try
            {
                DataAccess.DataServiceProvider dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();
                return dsp.GetProductActionRulePolicy(productOfferingID, action);
            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPDataError.ErrorQueryingSBERulesPolicyTableInDB), ex.Message);
            }

        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapName"></param>
        /// <param name="transformationXml"></param>
        /// <param name="isComplex"></param>
        /// <returns></returns>
        public DataSet GetDynamicMappingfromDatabase(string mapName, out string transformationXml, out int isComplex)
        {
            DataAccess.DataServiceProvider dsp;
            try
            {

                dsp = new CSF.Communications.Sdp.ServiceLogic.DataAccess.DataServiceProvider();

                return dsp.GetDynamicMappingfromDatabase(mapName,out transformationXml,out isComplex);
            }
            catch (Exception ex)
            {
                throw new SDPDataException(Convert.ToInt64(SDPSystemError.SDPDynamicMapTrnsformationException), ex.Message);
            }

        }
        #endregion
    }
}
